package com.wk1un2.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.entity.Sign;
import com.wk1un2.service.SignService;
import com.wk1un2.util.Result;

@RequestMapping("/public")
@RestController
public class SignController {
	@Autowired
	private SignService signservice;

	/**
	 * 查询
	 * 
	 * @return
	 */
	@RequestMapping("/getAllSign")
	public Object getAll(@RequestParam Map<String, Object> map) {
		Integer page = Integer.parseInt(map.get("page").toString());
		Integer limit = Integer.parseInt(map.get("limit").toString());
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		Result res = new Result(0, "success", signservice.count(map), signservice.getAll(map));
		return res;
	}

	/**
	 * 查询当天签到的咨询师
	 * 
	 * @return
	 */
	@RequestMapping("/getSignConsultant")
	public Object getSignConsultant(Integer page, Integer limit, Integer signstate, String username) {
		List<Sign> ls = signservice.getSignConsultant(page, limit, signstate, username);
		Result res = new Result(0, "success", ls.size(), ls);
		return res;
	}

	/**
	 * 基础添加
	 *
	 * @return
	 */
	@RequestMapping("/addSign")
	public Result insert(Sign sign) {
		Result res = new Result(0, "添加成功", signservice.insert(sign), true);
		return res;
	}

	/**
	 * 修改
	 *
	 * @return
	 */
	@RequestMapping("/updateSign")
	public Result updateById(Sign sign) {
		Result res = new Result(0, "修改成功", signservice.update(sign), true);
		return res;
	}

	/**
	 * 删除
	 *
	 * @return
	 */
	@RequestMapping("/deleteSign")
	public Result deleteById(Integer signid) {
		Result res = new Result(0, "删除成功", signservice.delete(signid), true);
		return res;
	}

	/**
	 * 签到
	 * 
	 * @return
	 */
	@RequestMapping("addSignMain")
	public Object addSignMain(@RequestParam Map<String, Object> map) {
		Integer userid = Integer.parseInt(map.get("userid").toString());
		Result res = null;
		System.out.println(signservice.getSignStateMain(userid));
		if (signservice.getSignStateMain(userid).size() == 0 || signservice.getSignStateMain(userid) == null) {
			res = new Result(0, "签到成功!!!", signservice.addSignMain(map), true);
		} else {
			res = new Result(0, "您今天已经签过到了！！！", -1, false);
		}
		return res;
	}

	/**
	 * 签退
	 * 
	 * @return
	 */
	@RequestMapping("updateByUseridMain")
	public Object updateByUseridMain(@RequestParam Map<String, Object> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		List<Sign> s = signservice.getSignStateMain(Integer.parseInt(map.get("userid").toString().toString()));
		int minute = getMinutesDiff(sdf.format(s.get(0).getSigntime()), sdf.format(new Date()));
		Result res = null;
		if (s.get(0).getSignstate().equals(2)) {
			res = new Result(0, "今天已经签退", -1, false);
		} else if (minute > 10) {
			res = new Result(0, "签退成功!!!", signservice.updateByUseridMain(map), true);

		} else {
			res = new Result(0, "签退失败,签到完成后最低十分钟才能签退！！！", -1, false);
		}
		System.out.println(minute);
		return res;
	}

	public static Integer getMinutesDiff(String startDate, String endDate) {
		int ret = 0;
		if (startDate.equals(null) || endDate.equals(null)) {
		} else {
			String startDateStr[] = startDate.split(":");
			String endDateStr[] = endDate.split(":");
			if (startDateStr[0].startsWith("0")) {
				startDateStr[0] = startDateStr[0].substring(1);
			}
			if (startDateStr[1].startsWith("0")) {
				startDateStr[1] = startDateStr[1].substring(1);
			}
			if (endDateStr[0].startsWith("0")) {
				endDateStr[0] = endDateStr[0].substring(1);
			}
			if (endDateStr[1].startsWith("0")) {
				endDateStr[1] = endDateStr[1].substring(1);
			}
			int s = Integer.parseInt(startDateStr[0]) * 60 + Integer.parseInt(startDateStr[1]);
			int e = Integer.parseInt(endDateStr[0]) * 60 + Integer.parseInt(endDateStr[1]);
			ret = e - s;
		}
		System.out.println(startDate);
		System.out.println(endDate);
		return ret;

	}

	/**
	 * 查询用户状态
	 * 
	 * @return
	 */
	@RequestMapping("getSignStateMain")
	public Object getSignStateMain(Integer userid) {
		Result res = new Result(0, "success", signservice.getSignStateMain(userid).size(),
				signservice.getSignStateMain(userid));
		return res;
	}

}
